#!/usr/bin/python
# -*- coding: UTF-8 -*-
import json
from kpautos.engine.case import Case
from kptest.config.enum import DeviceType, HostType, DeviceUtility
from extension.test_logic.RequestBase import RequestBase


class REDFISH_CPU_INFO_003(Case):
    """
        Name:
            REDFISH_CPU_INFO_003
        Label:
            CPU2信息
        PreCondition:
            OpenUBMC web service normal
        TestStep:
            1) Get  https://BMCIP/redfish/v1/Systems/1/processors/2
        ExpectedResult:
            A) 执行响应码为200..查看CPU信息：Manufacturer、Model、MaxSpeedMHz、TotalCores（与BMC- 系统管理- 处理器 下信息一致）
        Parameters:

        Author:
            baixin liuya
        Last Modified:

        Remark:

    """

    # 测试执行前配置等准备操作
    def pre_test_case(self):
        self.logger.info("Pre Test Case Start.... ")
        self.device_dut = self.resource.get_device(device_type=DeviceType.Server, utility=DeviceUtility.DUT)
        self.bmc_host = self.device_dut.get_host(host_type=HostType.BMC)
        self.bmc_ip = self.bmc_host.local_ip
        self.bmc_user = self.bmc_host.username
        self.bmc_pwd = self.bmc_host.password

        self.logger.info("获取Token")
        self.request = RequestBase(self.bmc_ip, self.bmc_user, self.bmc_pwd)
        return_code, session_id = self.request.create_session()
        self.assertIsNotNone(return_code)
        self.session_id = session_id

    # 测试步骤
    def procedure(self):
        self.log_step("Step1: 获取CPU2信息")
        resp = self.request.get("/redfish/v1/Systems/1/processors/2")
        self.assertIsNotNone(resp[0])
        content = resp[1]
        self.logger.info(f"接口[/redfish/v1/Systems/1/processors/2]返回值：\n{json.dumps(content, indent=4)}")
        key_info = "\nName: {}\nId: {}\nManufacturer: {}\nModel: {}\nMaxSpeedMHz:{}\nTotalCores: {}"
        self.log_step('Step2: CPU1信息' + key_info.format(content['Name'], content['Id'], content['Manufacturer'], \
                                                        content['Model'], content['MaxSpeedMHz'], \
                                                        content['TotalCores']))

    # 测试结束后恢复环境
    def post_test_case(self):
        resp = self.request.delete_session(self.session_id)
        self.assertIsNotNone(resp[0], "清除Token失败")
        self.logger.info("清除Token成功")
